// JavaScript Document


function fixShopcart(){
	var top = $('#shopcart-right').offset().top - parseFloat($('#shopcart-right').css('marginTop').replace(/auto/, 0)) - 10;
	//alert(top);
	$(window).scroll(function (event) {
	    // Y position of the scroll is
	    var y = $(this).scrollTop();
	  
	    // If Y pos below the shopcart, then add the 'shopcart fixed' class,
	    // otherwise remove
	    if (y >= top) {
	      $('#shopcart-right').addClass('shopcart-fixed');
	    } else {
	      $('#shopcart-right').removeClass('shopcart-fixed');
	    }
	  });
}

function toggleRadio(){	
	$('#radio-1').change(function(){
		$(this).next().addClass('input-checked');
		$('#rd-text-2').removeClass('input-checked');		
	});
	
	$('#radio-2').change(function(){
		$(this).next().addClass('input-checked');
		$('#rd-text-1').removeClass('input-checked');		
	});
}


$(document).ready(function(){
	var wHeight = $(window).height();
	var wWidth = $(window).width();
	var timeAni = 250;
	
	//Homepage slideshow
	$('#slider-homepage').cycle({ 
		fx:    'scrollLeft', 
    	delay: -1000,
		speed:1000,
		timeout:3000,
		pager:  '#slider-paginator',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#slider-paginator li:eq(' + (idx) + ') a';
        }
	});
	
	//Datepicker
	$('.datepicker').datepicker({
		dateFormat  : 'dd.MM yy',
		"option"	:$.datepicker.regional[ "da" ]});
	
	// Show cake info
	$('.eachCake-image').showFeatureText();
	
	//Zoom image
	$('.zoomImage').prettyPhoto();
	
	//Toggle Radio
	toggleRadio();
	
	//Hover show 'X' on each Item  in shopcart in product page
	$(".eachItem-cart").live({
        mouseenter:
        	function(){        		
        		$(this).animate({backgroundColor:'#1e1e1e'},'fast');
        		$(this).find('a.eachItem-close').fadeIn('normal');
        		$(this).find('.eachItem-option').find('input').animate({backgroundColor:'#1e1e1e'},'fast');
        		//$(this).find('.eachItem-option').find('input').css({'backgroundColor':'#1e1e1e'});
        	},
        mouseleave:
        	function(){        	
        		$(this).animate({backgroundColor:'#000000'},'fast');
        		$(this).find('a.eachItem-close').fadeOut('normal');
        		//$(this).find('.eachItem-option').find('input').css({'backgroundColor':'#000000'});
        		$(this).find('.eachItem-option').find('input').animate({backgroundColor:'#000000'},'fast');
        	}
    });
	
	//Hover show 'X' on each Item  in shopcart in SHOPCART
	$(".eachOrder").live({
        mouseenter:
        	function(){
        		$(this).find('a.eachOrder-close').fadeIn('fast');
        		$(this).find('.eachOrder-option').find('input').css({'backgroundColor':'#1e1e1e'});
        	},
        mouseleave:
        	function(){
        		$(this).find('a.eachOrder-close').fadeOut('fast');
        		$(this).find('.eachOrder-option').find('input').css({'backgroundColor':'#000000'});
        	}
    });

	// Remove the selected item in shopcart in PRODUCT PAGE
	$('.eachItem-close').live("click",function(){
		var $eachParent = $(this).parent('.eachItem-cart');		
		$eachParent.slideUp('fast',function(){ $eachParent.remove();});
		
	});

	// Remove the selected item in shopcart in SHOPCART PAGE
	$('.eachOrder-close').live("click",function(){
		var $eachParent = $(this).parent('.eachOrder');		
		$eachParent.slideUp('fast',function(){ $eachParent.remove();});
		
	});
	
	
	// Remove all items in cart
	
	$('#delete-all-yes').click(function(){		
		$('.shopcart-list').slideUp(function(){
			$('.shopcart-list').html('').css({'display':'block'});
			$('.shopcart-total').find('span').html('0 DKK');			
		});		
	});
	
	// Change item quantity
	$('.eachItem-up-down, .eachOrder-up-down').live("click",function(){
		
		//Retrieve the background color
		//$bg = $(this).parent().parent().parent('.eachItem-cart').css('backgroundColor');
		
		//Retrieve the input in the same item
		$input = $(this).parent().siblings('input');
		
		//Split the first and 2nd in multi class tag
		//Only use the first idx,
		$class = $(this).attr('class').split(' ');
		
		if($class[0] == 'eachItem-up' || $class[0] == 'eachOrder-up'){			
			// Animate color
			$input.animate({backgroundColor: '#bf141d'},timeAni,function(){
				if($class[0] == 'eachItem-up')
					$bg = $(this).parent().parent('.eachItem-cart').css('backgroundColor');				
				else					
					$bg = $(this).parent().parent('.eachOrder').css('backgroundColor');
				
				$input.animate({backgroundColor:$bg},timeAni);
			});
			$input.val(parseInt($input.val())+1);
		}else{			
			// Get the value after minus 1
			var a = parseInt($input.val())-1;
			
			// If more than 0, then keep the itemcart (minium = 1)
			// Otherwise, remove
			if(a > 0){				
				$input.val(a);
				// Animate color
				$input.animate({backgroundColor: '#bf141d'},timeAni,function(){
					if($class[0] == 'eachItem-down')
						$bg = $(this).parent().parent('.eachItem-cart').css('backgroundColor');
					else
						$bg = $(this).parent().parent('.eachOrder').css('backgroundColor');
					$input.animate({backgroundColor:$bg},timeAni);
				});
			}
			/*
			else{				
				//Get the item parent based on where the shopcart is
				if($class[0] == 'eachItem-down')
					$parent = $(this).parent().parent().parent('.eachItem-cart');
				else $parent = $(this).parent().parent().parent('.eachOrder');
				$parent.slideUp(function(){ $parent.remove();});
			}*/			
		}
		//alert($class[0]);
				
	});
	
	// Add item into shopcart
	$('.eachCake-order').click(function(){		
		createItemDOM();
	});
	
	// Fix shopcart position when scrolling down
	fixShopcart();
	
	//Toggle text input with class "txtInput_toggle"
	$('.txtInput_toggle').focus(function() {
		value=$(this).val();
		$(this).attr("value","");
	});
	$('.txtInput_toggle').blur(function() {
		if($(this).val()=="") {
			$(this).val(value);
		}
	});	
	
});

$.fn.showFeatureText = function() {
	return this.each(function(){    
		var box = $(this);
		var text = $('p',this);    
	
		text.css({ position: 'absolute', bottom: '0px' }).hide();
	
		box.hover(function(){
		  text.slideDown("slow");
		},function(){
		  text.slideUp("slow");
		});			
  });
}



function createItemDOM(){
	
	$itemWrapper = $('<div style="display:none">').addClass('eachItem-cart border-1');
		$close = $('<a class="eachItem-close" id="close-id">X</a>');
		$p1 = $('<p>').text('Avocado og Laks');
		$span1 = $('<span>').text('Small maki');
		$innerOption = $('<div class="eachItem-option clear-fix">');
			$divTemp1 = $('<div class="eachItem-button">');
				$up = $('<a class="eachItem-up eachItem-up-down">Up</a>');
				$down = $('<a class="eachItem-down eachItem-up-down">Down</a>');
				$clear = $('<div class="clear">');
			$input = $('<input type="text" class="border-5"/>').attr({
				name:'test',
				value:'1'
			});
			$span2 = $('<span>').text('stk');
			$p2 = $('<p>').text('209 DKK');
	
	$up.appendTo($divTemp1);	
	$down.appendTo($divTemp1);	
	$clear.appendTo($divTemp1);
	
	
	$divTemp1.appendTo($innerOption);
	
	$input.appendTo($innerOption);
	$span2.appendTo($innerOption);
	$p2.appendTo($innerOption);
	//alert('a');
	$close.appendTo($itemWrapper);
	$p1.appendTo($itemWrapper);
	$span1.appendTo($itemWrapper);
	$innerOption.appendTo($itemWrapper);
			
	$('.shopcart-list').append($itemWrapper);
	// Add this line to make it slide
	$('.shopcart-list').find(".eachItem-cart:last").slideDown("fast");
	//alert('b');
}
